package io.gitee.zhucan123.core.infrastructure.mvc;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zhuCan
 * @date: 2019/11/22 10:41
 * @description:
 */
@Component
public class PageableAssembler implements PageFunction<Pageable> {
    @Override
    public PageRequest apply(Pageable pageable) {
        return PageRequest.of(pageable.getPageNumber() - 1, pageable.getPageSize(), pageable.getSort());
    }

    /**
     * 实现将sort转化成List<String>的数组
     */
    public List<String> getSortListString(Pageable pageable) {
        Sort sort = pageable.getSort();
        if (sort.isSorted()) {
            return sort.get().map(x -> x.getProperty()+ ',' +x.getDirection()).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
